package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsArticleMapper;
import com.qf.pojo.DtsArticle;
import com.qf.service.DtsArticleService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/12/5 15:16
 * description:推广管理 公告列表
 */
@Service
public class DtsArticleServiceImpl implements DtsArticleService {
    @Autowired
    private DtsArticleMapper articleMapper;
    //公告列表
    @Override
    public IPage<DtsArticle> findArticleAllList(String title, Integer page, Integer limit, String sort, String order) {
        IPage<DtsArticle> ipage = new Page(page,limit);
        QueryWrapper<DtsArticle> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        wrapper.eq("deleted",false);
        if("asc".equals(order)){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }
        IPage<DtsArticle> p = articleMapper.selectPage(ipage, wrapper);
        return p;
    }
    //公告详情
    @Override
    public Object findArticleById(Integer id) {
        DtsArticle article = articleMapper.selectById(id);
        return ResponseUtil.ok(article);
    }
    //公告添加
    @Override
    public Object insert(DtsArticle article) {
        LambdaQueryWrapper<DtsArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsArticle::getTitle,article.getTitle());
        wrapper.eq(DtsArticle::getDeleted,false);
        DtsArticle article1 = articleMapper.selectOne(wrapper);
        if(article1 != null){
            return ResponseUtil.fail(-1,"标题已存在");
        }
        article.setAddTime(new Date());
        return articleMapper.insert(article) > 0 ? ResponseUtil.ok() : ResponseUtil.fail(-1,"添加失败");

    }
    //公告删除
    @Override
    public Object delete(DtsArticle article) {
        return articleMapper.deleteById(article) > 0 ? ResponseUtil.ok() : ResponseUtil.fail(-1,"删除失败");

    }
    //公告修改
    @Override
    public Object update(DtsArticle article) {
        DtsArticle article1 = articleMapper.selectById(article);
        if(!article1.getTitle().equals(article.getTitle())){
            LambdaQueryWrapper<DtsArticle> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DtsArticle::getTitle,article.getTitle());
            wrapper.eq(DtsArticle::getDeleted,false);
            wrapper.select(DtsArticle::getId);
            wrapper.last("limit 1");
            DtsArticle article2 = articleMapper.selectOne(wrapper);
            if(article2 != null){
                return ResponseUtil.fail(-1,"标题已存在");
            }
        }
        article.setUpdateTime(new Date());
        article.setDeleted(false);
        return articleMapper.updateById(article) > 0 ? ResponseUtil.ok() : ResponseUtil.fail(-1,"修改失败");
    }
}
